/**
 * Created by wqz806687102 on 2016/1/19.
 */
'use strict';


angular.module('QC').controller('QualityRecordController', ['$scope', '$state', 'BaseModule', 'BaseEnum', 'BaseFunction', 'ExtendEnumService', '$location', 'Authentication',
    function($scope, $state, BaseModule, BaseEnum, BaseFunction, ExtendEnumService, $location, Authentication) {

        $scope.BarcodeMaster;
        $scope.BarcodeMasterID = "0";
        $scope.QCStatusEnum;
        ExtendEnumService.getExtendEnum('QCStatus', function(extendEnum) {
            $scope.QCStatusEnum = extendEnum;
        });
        $scope.QCTypeEnum;
        ExtendEnumService.getExtendEnum('QCType', function(extendEnum) {
            $scope.QCTypeEnum = extendEnum;
        });

        $scope.BadTypeEnum;
        ExtendEnumService.getExtendEnum('BadType', function(extendEnum) {
            $scope.BadTypeEnum = extendEnum;
        });
        $scope.BadPositionEnum;
        ExtendEnumService.getExtendEnum('BadPosition', function(extendEnum) {
            $scope.BadPositionEnum = extendEnum;
        });

        $scope.btnSave = function() {
            for (var i = 0; i < $scope.Module.QCRecord.CacheRecords.length; i++) {
                if ($scope.Module.QCRecord.CacheRecords[i].BizRecord === $scope.Module.BizRecord.FocusedRecord._id) {
                    $scope.Module.QCRecord.removeRecord($scope.Module.QCRecord.Records[i]);
                    i--;
                }
            };
            $scope.Module.QCRecord.Records = $scope.Module.QCRecord.CacheRecords.concat($scope.Module._QCRecord.CacheRecords);
            $scope.Module.QCRecord.save(function() {
                $scope.Module.BizRecord.save(function() {
                    //$scope.Module.BizRecord.load();
                });

            });
        };
        $scope.btnCancel = function() {
            $state.reload();
        };
        //查询
        $scope.btnQuery = function() {
            if ($scope.BarcodeMaster == undefined || $scope.BarcodeMaster.trim() == "") {
                BaseFunction.alert("芯片ID不能为空！", "查询错误");
                return;
            };
            $scope.Module.BarcodeMaster.find({
                Barcode: $scope.BarcodeMaster
            }, null, null, function(barcodeRcd) {
                if (barcodeRcd.length < 0) {
                    BaseFunction.alert("根据芯片ID未找到条码主档！", "查询错误");
                } else {
                    $scope.BarcodeMasterID = barcodeRcd[0]._id;
                    $scope.Module.BizRecord.find({
                        $and: [{
                            BarcodeMaster: barcodeRcd[0]._id
                        }, {
                            QCType: {
                                '$ne': null
                            }
                        }]
                    }, {
                        CreatedOn: -1
                    }, $scope.Module.BizRecord.Populate, function(records) {
                        $scope.Module.BizRecord.Records = [];
                        $scope.Module.QCRecord.Records = [];
                        $scope.Module._QCRecord.Records = [];
                        $scope.Module.BizRecord.Records = records;
                        $scope.Module.BizRecord.emitOnLoadEvent();
                    });
                }
            });
        };
        //验证指标值
        $scope.cellValueChange = function(columnName, newValue, oldValue) {
            if (columnName === "NormValue" && $scope.Module._QCRecord.FocusedRecord.QCNorm.Type === 0) {
                var regInt = /^[-+]?\d*$/;
                var flInt = regInt.test(this.Module._QCRecord.FocusedRecord.NormValue.trim());
                var regDeci = /^[-\+]?\d+(\.\d+)?$/;
                var flDeci = regDeci.test(this.Module._QCRecord.FocusedRecord.NormValue.trim());
                if (!flInt && !flDeci) {
                    this.Module._QCRecord.FocusedRecord.NormValue = 0;

                    BaseFunction.alert("当前指标类型的指标值只能为数字和小数！", "输入错误");
                    $scope.$digest();
                }
            };

        };
        var oldBRFocusRecord = null;
        $scope.rowChange = function() {
            if (oldBRFocusRecord != null) {
                for (var i = 0; i < $scope.Module.QCRecord.CacheRecords.length; i++) {
                    if ($scope.Module.QCRecord.CacheRecords[i].BizRecord === oldBRFocusRecord._id) {
                        $scope.Module.QCRecord.removeRecord($scope.Module.QCRecord.CacheRecords[i]);
                        i--;
                    }
                }
                $scope.Module.QCRecord.Records = $scope.Module.QCRecord.CacheRecords.concat($scope.Module._QCRecord.CacheRecords);
            }
            oldBRFocusRecord = $scope.Module.BizRecord.FocusedRecord;
            $scope.Module._QCRecord.Records = [];
            if ($scope.Module.BizRecord.FocusedRecord.IsLoadQCRecord) {
                var _QCRecordRecords = [];
                for (var i = 0; i < $scope.Module.QCRecord.Records.length; i++) {
                    if ($scope.Module.QCRecord.Records[i].BizRecord == $scope.Module.BizRecord.FocusedRecord._id) {
                        _QCRecordRecords.push($scope.Module.QCRecord.Records[i]);
                    };
                }
                $scope.Module._QCRecord.Records = _QCRecordRecords;
                $scope.Module._QCRecord.emitOnLoadEvent();
            } else {
                $scope.Module.QCRecord.find({
                    BizRecord: $scope.Module.BizRecord.FocusedRecord._id
                }, null, $scope.Module.QCRecord.Populate, function(records) {
                    $scope.Module.BizRecord.FocusedRecord.IsLoadQCRecord = true;
                    $scope.Module._QCRecord.Records = records;
                    $scope.Module._QCRecord.emitOnLoadEvent();
                    $scope.Module.QCRecord.Records = $scope.Module.QCRecord.CacheRecords.concat(records);
                });
            };
        };

        BaseModule.initModule($scope, 'BizRecord');
        $scope.Module.BizRecord.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.BizRecord.PageSize = 15;
        // $scope.Module.BizRecord.Condition = {
        //     QCType: {
        //         $gte: 0
        //     }
        // };
        $scope.Module.BizRecord.Condition = {
            BarcodeMaster: $scope.BarcodeMasterID,
            QCType: {
                $gte: 0
            }
        };
        $scope.Module.BizRecord.Populate = [{
            path: 'Operation',
            select: 'Name'
        }, {
            path: 'QCUser',
            select: 'Code Name'
        }];

        BaseModule.initModule($scope, 'BarcodeMaster');
        BaseModule.initModule($scope, 'QCRecord');
        $scope.Module.QCRecord.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.QCRecord.Populate = {
            path: 'QCNorm',
            select: 'Name Type'
        };
        $scope.Module.QCRecord.PageSize = 15;

        BaseModule.initModule($scope, '_QCRecord');
        $scope.Module._QCRecord.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
        $scope.Module._QCRecord.PageSize = 15;
    }
]);